package com.raft.framework.utils

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.WindowManager


object UiUtils {

    private const val  SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT = 0x00000010

    private var sStatusBarHeight: Int? = null

    private var inited = false

    private var miui:Boolean = false
    private var meizu:Boolean = false
    private var oppo:Boolean = false

    fun setStatusBarLight(activity: Activity, light: Boolean) {
        if (VERSION.SDK_INT >= VERSION_CODES.M) {

            activity.window.decorView.let {

                //需要给根view加上全屏属性
                var systemUiVisibility = it.systemUiVisibility or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                systemUiVisibility = if (light) {
                    // |
                    systemUiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    // & ~
                    systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()
                }

                it.systemUiVisibility = systemUiVisibility

            }

        }else{
            if (!inited){
                setMIUIStatusBarDarkIcon(activity, light)
                setMeizuStatusBarDarkIcon(activity, light)
                setOPPOStatusBar(activity,light)
                inited = true
            }

            when {
                miui -> setMIUIStatusBarDarkIcon(activity, light)
                meizu -> setMeizuStatusBarDarkIcon(activity, light)
                oppo -> setOPPOStatusBar(activity,light)
            }

        }



    }

    private fun setMIUIStatusBarDarkIcon(activity: Activity, darkIcon: Boolean):Boolean {

        try {
            val clazz = activity.window.javaClass
            val layoutParams = Class.forName("android.view.MiuiWindowManager\$LayoutParams")
            val field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE")
            val darkModeFlag = field.getInt(layoutParams)
            val extraFlagField = clazz.getMethod("setExtraFlags", Int::class.javaPrimitiveType, Int::class.javaPrimitiveType)
            extraFlagField.invoke(activity.window, if (darkIcon) darkModeFlag else 0, darkModeFlag)
            miui = true
            return true
        } catch (e: Exception) {
            //e.printStackTrace();
            return false
        }
    }

    /**
     * 修改魅族状态栏字体颜色 Flyme 4.0
     */
    private fun setMeizuStatusBarDarkIcon( activity: Activity, darkIcon: Boolean):Boolean {
        try {
            val lp = activity.window.attributes
            val darkFlag = WindowManager.LayoutParams::class.java.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON")
            val meizuFlags = WindowManager.LayoutParams::class.java.getDeclaredField("meizuFlags")
            darkFlag.isAccessible = true
            meizuFlags.isAccessible = true
            val bit = darkFlag.getInt(null)
            var value = meizuFlags.getInt(lp)
            if (darkIcon) {
                value = value or bit
            } else {
                value = value and bit.inv()
            }
            meizuFlags.setInt(lp, value)
            activity.window.attributes = lp
            meizu = true
            return true
        } catch (e: Exception) {
            //e.printStackTrace();
            return false
        }
    }

    private fun setOPPOStatusBar(activity: Activity, darkIcon: Boolean) : Boolean{

        if (Build.MANUFACTURER.equals("OPPO",true)) {

            activity.window.decorView?.let {

                var systemUiVisibility = it.systemUiVisibility

                systemUiVisibility = if (darkIcon) {
                    systemUiVisibility or SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT
                } else {
                    systemUiVisibility and SYSTEM_UI_FLAG_OP_STATUS_BAR_TINT.inv()
                }
                it.systemUiVisibility = systemUiVisibility
            }
            oppo = true
            return true
        }
        return false
    }


    fun getStatusBarHeight(context: Context): Int {
        if (sStatusBarHeight == null) {
            try {
                val resId = context.resources
                        .getIdentifier("status_bar_height", "dimen", "android")
                sStatusBarHeight = context.resources.getDimensionPixelSize(resId)
            } catch (e: Exception) {
                e.printStackTrace()
            }

            if (sStatusBarHeight == null || sStatusBarHeight == 0) {
                sStatusBarHeight = UICompat.dp2px(25)
            }

        }
        return sStatusBarHeight?:0
    }


    fun injectStatusBar(view: View){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            view.setPadding(0, getStatusBarHeight(view.context), 0, 0)
        }
    }

    fun injectSpaceStatusBarHeight(view: View,h:Int = 0){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            view.layoutParams?.run {
                height = if (h == 0){
                    getStatusBarHeight(view.context)
                }else{
                    h
                }
                //height = getStatusBarHeight(view.context)

                view.layoutParams = this
            }
        }
    }


    fun addOnGlobalLayoutListener(view: View?,listener: ViewTreeObserver.OnGlobalLayoutListener?){

        view?.viewTreeObserver?.addOnGlobalLayoutListener (object : ViewTreeObserver.OnGlobalLayoutListener{
            override fun onGlobalLayout() {

                listener?.onGlobalLayout()

                view.viewTreeObserver.removeOnGlobalLayoutListener(this)

            }

        })

    }

    fun addOnGlobalLayoutListener(view: View?,listener: ((View)->Unit)?){

        view?.viewTreeObserver?.addOnGlobalLayoutListener (object : ViewTreeObserver.OnGlobalLayoutListener{
            override fun onGlobalLayout() {

                listener?.invoke(view)

                view.viewTreeObserver.removeOnGlobalLayoutListener(this)

            }

        })

    }
}
